cmake_minimum_required(VERSION 3.26)

project(AvatarEditor VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 REQUIRED COMPONENTS Widgets Network Xml OPTIONAL_COMPONENTS PrintSupport)

set(PROJECT_SOURCES
    main.cpp
    mainwindow.cpp
    mainwindow.h
    mainwindow.ui
)

qt_add_executable(${PROJECT_NAME}
    MANUAL_FINALIZATION
    ${PROJECT_SOURCES}
    AvatarLibrary.h AvatarLibrary.cpp
    Program.h Program.cpp
    entity/EntityCamera.h
    entity/EntityLight.h
    entity/EntitySceneNode.h
    entity/EntityMaterial.h
    entity/EntityPhysical.h
    entity/EntityPlugin.h
    entity/Vector3.h
    entity/Vector2.h
    Configure.h
    utility/ProjectReader.h utility/ProjectReader.cpp
    utility/ProjectWriter.h utility/ProjectWriter.cpp
    utility/SceneExport.h utility/SceneExport.cpp
    utility/AvatarLuaLexer.h utility/AvatarLuaLexer.cpp
    utility/DocProcess.h utility/DocProcess.cpp
    utility/Common.h utility/Common.cpp
    widget/widget_scene_tree.h widget/widget_scene_tree.cpp widget/widget_scene_tree.ui
    widget/widget_script_tree.h widget/widget_script_tree.cpp widget/widget_script_tree.ui
    widget/widget_script_editor.h widget/widget_script_editor.cpp widget/widget_script_editor.ui
    widget/widget_edit_node.h widget/widget_edit_node.cpp widget/widget_edit_node.ui
    widget/widget_edit_gui.h widget/widget_edit_gui.cpp widget/widget_edit_gui.ui
    widget/widget_node_transform.h widget/widget_node_transform.cpp widget/widget_node_transform.ui
    widget/widget_node_material.h widget/widget_node_material.cpp widget/widget_node_material.ui
    widget/widget_node_physical.h widget/widget_node_physical.cpp widget/widget_node_physical.ui
    widget/widget_node_attachment.h widget/widget_node_attachment.cpp widget/widget_node_attachment.ui
    widget/widget_node_animation.h widget/widget_node_animation.cpp widget/widget_node_animation.ui
    widget/widget_avatar_view.h widget/widget_avatar_view.cpp
    widget/widget_log_text.h widget/widget_log_text.cpp
    widget/input/widget_input.h widget/input/widget_input.cpp
    widget/input/input_text.h widget/input/input_text.cpp
    widget/input/input_file.h widget/input/input_file.cpp
    widget/input/input_color.h widget/input/input_color.cpp
    widget/input/input_check.h widget/input/input_check.cpp
    widget/input/input_combo.h widget/input/input_combo.cpp
    dialog/dialog_publish.h dialog/dialog_publish.cpp dialog/dialog_publish.ui
    dialog/dialog_setup_plugin.h dialog/dialog_setup_plugin.cpp dialog/dialog_setup_plugin.ui
    dialog/dialog_setup_scene.h dialog/dialog_setup_scene.cpp dialog/dialog_setup_scene.ui
    resources.qrc
    AvatarEditor.rc
)

if (CMAKE_SYSTEM_NAME MATCHES "Linux")
    set(AVATAR_HOME "/home/luoyexunxue/Code/avatar")
    set(AVATAR_LIB ${AVATAR_HOME}/bin/${CMAKE_BUILD_TYPE}/libavatar.so)
    set(AVATAR_LIB_DEPENDS ${AVATAR_HOME}/bin/${CMAKE_BUILD_TYPE}/libavatar.so)
elseif (CMAKE_SYSTEM_NAME MATCHES "Windows")
    set(AVATAR_HOME "E:/Avatar/Avatar")
    set(AVATAR_LIB ${AVATAR_HOME}/bin/${CMAKE_BUILD_TYPE}/lib/avatar.lib)
    set(AVATAR_LIB_DEPENDS
        ${AVATAR_HOME}/bin/${CMAKE_BUILD_TYPE}/avatar.dll
        ${AVATAR_HOME}/extension/winx64/avcodec-57.dll
        ${AVATAR_HOME}/extension/winx64/avdevice-57.dll
        ${AVATAR_HOME}/extension/winx64/avfilter-6.dll
        ${AVATAR_HOME}/extension/winx64/avformat-57.dll
        ${AVATAR_HOME}/extension/winx64/avutil-55.dll
        ${AVATAR_HOME}/extension/winx64/freetype.dll
        ${AVATAR_HOME}/extension/winx64/gdal204.dll
        ${AVATAR_HOME}/extension/winx64/glew32.dll
        ${AVATAR_HOME}/extension/winx64/libcurl.dll
        ${AVATAR_HOME}/extension/winx64/libpng16.dll
        ${AVATAR_HOME}/extension/winx64/OpenAL32.dll
        ${AVATAR_HOME}/extension/winx64/opencv_world455.dll
        ${AVATAR_HOME}/extension/winx64/postproc-54.dll
        ${AVATAR_HOME}/extension/winx64/swresample-2.dll
        ${AVATAR_HOME}/extension/winx64/swscale-4.dll
        ${AVATAR_HOME}/extension/winx64/VideoCap.dll
        ${AVATAR_HOME}/extension/winx64/VideoPlay.dll
        ${AVATAR_HOME}/extension/winx64/zlib.dll
    )
endif()

set(AVATAR_DIR "${AVATAR_HOME}/Avatar")
set(AVATAR_PLUGINS_DIR "${AVATAR_HOME}/bin/${CMAKE_BUILD_TYPE}/plugins")

add_dependencies(${PROJECT_NAME} QScintilla)
target_compile_definitions(${PROJECT_NAME} PRIVATE QSCINTILLA_DLL)

target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${QScintilla_SOURCE_DIR}/src ${AVATAR_DIR})
target_link_libraries(${PROJECT_NAME} PRIVATE
    Qt6::Widgets
    Qt6::Network
    Qt6::Xml
    QScintilla
    ${AVATAR_LIB}
)

if(TARGET Qt6::PrintSupport)
    target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::PrintSupport)
    target_compile_definitions(${PROJECT_NAME} PRIVATE QT_PRINTSUPPORT)
endif()

add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_if_different ${AVATAR_LIB_DEPENDS} $<TARGET_FILE_DIR:${PROJECT_NAME}>
    COMMAND ${CMAKE_COMMAND} -E copy_directory_if_different ${AVATAR_PLUGINS_DIR} "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins"
)

set_target_properties(${PROJECT_NAME} PROPERTIES
    ${BUNDLE_ID_OPTION}
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

include(GNUInstallDirs)
install(TARGETS ${PROJECT_NAME}
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

qt_finalize_executable(${PROJECT_NAME})
